package com.zhongxi.guoyuan.view.personalCenter;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.personCenter.BoxAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.user.BoxData;
import com.zhongxi.guoyuan.entity.user.BoxResult;
import com.zhongxi.guoyuan.entity.user.center.UserCenterResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.MainActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BoxActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2{

    private TextView tvBeautiful;
    private TextView tvCoin;
    private TextView tvRedBag;
    private int type = 1;
    private PullToRefreshListView lvList;
    private List<BoxData> data = new ArrayList<BoxData>();
    private BoxAdapter adapter;
    private TextView tvRest;
    private TextView tvGoStore;
    private TextView tvTips;
    private TextView tvTip;
    private Map<String, String> map = new HashMap<>();
    private String flag;
    public static final int resultBoxSiler=44;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_box);
        initTitle();
        tvTitle.setText("宝箱");
        type = getIntent().getIntExtra("index", 1);
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        tvBeautiful = (TextView) findViewById(R.id.tvBeautiful);
        tvCoin = (TextView) findViewById(R.id.tvCoin);
        tvRest = (TextView) findViewById(R.id.tvRest);
        tvTips = (TextView) findViewById(R.id.tvTips);
        tvTip = (TextView) findViewById(R.id.tvTip);
        tvRedBag = (TextView) findViewById(R.id.tvRedBag);
        tvGoStore = (TextView) findViewById(R.id.tvGoStore);
        switch (type){
            case 1:
                tvBeautiful.setSelected(true);
                tvGoStore.setText("去美容院");
                tvTip.setVisibility(View.GONE);
                break;
            case 2:
                tvRedBag.setSelected(true);
                tvGoStore.setText("提现");
                tvTip.setVisibility(View.VISIBLE);
                break;
            case 3:
                tvCoin.setSelected(true);
                tvGoStore.setText("去消费");
                tvTip.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new BoxAdapter(context, data);
        lvList.setAdapter(adapter);
        getUserInfo();
    }

    /**
     * 获取用户信息
     */
    private void getUserInfo() {
        flag = "userinfo";
        getServer(Url.USER_CENTER, null, "", false, null, "get");
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        tvBeautiful.setOnClickListener(this);
        tvCoin.setOnClickListener(this);
        tvRedBag.setOnClickListener(this);
        tvGoStore.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvBeautiful:
                type = 1;
                pageNum = 1;
                tvBeautiful.setSelected(true);
                tvCoin.setSelected(false);
                tvRedBag.setSelected(false);
                tvGoStore.setText("去美容院");
                tvTip.setVisibility(View.GONE);
                getData();
                break;
            case R.id.tvCoin:
                type = 3;
                pageNum = 1;
                tvBeautiful.setSelected(false);
                tvCoin.setSelected(true);
                tvRedBag.setSelected(false);
                tvGoStore.setText("去银币消费");
                tvTip.setVisibility(View.GONE);
                getData();
                break;
            case R.id.tvRedBag:
                type = 2;
                pageNum = 1;
                tvBeautiful.setSelected(false);
                tvCoin.setSelected(false);
                tvRedBag.setSelected(true);
                tvGoStore.setText("提现");
                tvTip.setVisibility(View.VISIBLE);
                getData();
                break;
            case R.id.tvGoStore:
                if(type == 1){
                    //去美容院首页
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.putExtra("index",0);
                    startActivity(intent);
                }else if(type == 2){
                    startAct(WithDrawCashActivity.class);
                }else if(type == 3){
                    //去银币消费商家首页
                    Intent intent = new Intent(context, MainActivity.class);
                    intent.putExtra("index",2);
                    startActivity(intent);
                }
                break;
        }
    }

    private void getData(){
        flag = "data";
        String str = "&page="+pageNum+"&welfaretype="+type;
        getServer(Url.MY_TREASURE_CHEST, null, str, false, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("data".equals(flag)) {
                BoxResult result = gson.fromJson(response, BoxResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                switch (type) {
                    case 1:
                        tvTips.setText("美币余量：");
                        tvRest.setText(MyApplication.getInstance().getInnerCurrency() + "美币");
                        break;
                    case 2:
                        tvTips.setText("现金余量：");
                        tvRest.setText(MyApplication.getInstance().getRmb() + "元");
                        break;
                    case 3:
                        tvTips.setText("银币余量：");
                        tvRest.setText(MyApplication.getInstance().getSilver() + "银币");
                        break;
                }
                if (pageNum == 1) {
                    data.clear();
                }
                if (result.getData().size() > 0) {
                    data.addAll(result.getData());
                }
                adapter.setType(type);
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }else if("userinfo".equals(flag)){
                UserCenterResult result = gson.fromJson(response, UserCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);
                sharedPrefrences.edit().putString("rmb", result.getUserExt().getRmb()).commit();
                sharedPrefrences.edit().putString("silver", result.getUserExt().getSilver()).commit();
                sharedPrefrences.edit().putString("inner", result.getUserExt().getInnercurrency()).commit();
                sharedPrefrences.edit().putString("avatar", result.getUserInfo().getAvatar()).commit();
                if("1".equals(result.getUserInfo().getIsprivilege())){
                    sharedPrefrences.edit().putBoolean("privilege", true).commit();
                    Boolean a = MyApplication.getInstance().isPrivilege();
                }
                if("1".equals(result.getUserInfo().getIsshareholders())){
                    sharedPrefrences.edit().putBoolean("shareHolder", true).commit();
                }
                getData();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        pageNum = 1;
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        pageNum++;
        getData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        setResult(resultBoxSiler);
    }
}
